﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Petroleum.Tool;
using HZH_Controls.Controls;
using Petroleum.DB.Model;
using System.Text.RegularExpressions;
using Petroleum.UC.Public;
using Petroleum.DB.PetroleumModel;
using Petroleum.Model;
using Newtonsoft.Json;
using System.Data.SQLite;
using HZH_Controls.Forms;

namespace Petroleum.UC
{
    public partial class Step10 : UserControl
    {
        FormMain main = new FormMain();
        HydraulicData Hydraulic = new HydraulicData();
        List<HydraulicModel> hydraulics = new List<HydraulicModel>();
        public Step10(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            //this.dgv.Headers.AddHeader(2, 3, 0, 0, "Interval  (MD, m)");
            //this.dgv.Headers.AddHeader(5, 8, 0, 1, "Drilling Parameters");
            //this.dgv.Headers.AddHeader(9, 13, 0, 1, "Hydraulic Parameters");
            //dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            //dgv.ColumnHeadersHeight = 46;
            picConductor.Initialization("Conductor Hydraulic");
            pic1st.Initialization("1st Spud Hydraulic Parameters");
            pic2nd.Initialization("2nd Spud Hydraulic Parameters");
            //this.dgv.DataSource = hydraulics;
        }
        void SaveData()
        {
            hydraulics.Clear();
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                var val = dgv.Rows[i].Cells["SpudNo"].Value;
                if (val == null || val == "")
                    continue;
                hydraulics.Add(new HydraulicModel()
                {
                    SpudNo = GetCell(i, "SpudNo"),
                    Bittype = GetCell(i, "Bittype"),
                    Interval = GetCell(i, "Interval"),
                    Nozzle = GetCell(i, "Nozzle"),
                    Density = GetCell(i, "Density"),
                    PumpRate = GetCell(i, "PumpRate"),
                    SSP = GetCell(i, "SSP"),
                    Pumpradio = GetCell(i, "Pumpradio"),
                    Bitpressure = GetCell(i, "Bitpressure"),
                    Impact = GetCell(i, "Impact"),
                    Jetvelocity = GetCell(i, "Jetvelocity"),
                    BitHydraulic = GetCell(i, "BitHydraulic"),
                    HSIradio = GetCell(i, "HSIradio"),
                    Upward = GetCell(i, "Upward"),
                });
            }
            Session.Hydraulic.imgHydraulic1 = picConductor.ImgUrl;
            Session.Hydraulic.imgHydraulic2 = pic1st.ImgUrl;
            Session.Hydraulic.imgHydraulic3 = pic2nd.ImgUrl;
            Session.Hydraulic.hydraulics = hydraulics;
        }
        string GetCell(int RowIndex, string CellName)
        {
            var cel = dgv.Rows[RowIndex].Cells[CellName].Value;
            if (cel != null)
            {
                string value = cel.ToString();
                return string.IsNullOrEmpty(value) ? "" : value.Trim();
            }
            return "";
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            SaveData();
            main.AddControl(new Step11(main));
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step9BitTypesDesign(main));
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            try
            {
                var casing = main.projects.Where(n => n.TableName == TableNames.Hydraulic.ToString()).FirstOrDefault();
                if (casing != null)
                {
                    Session.Hydraulic = JsonConvert.DeserializeObject<HydraulicData>(casing.TableData);
                    this.dgv.DataSource = Session.Hydraulic.hydraulics;
                    if (!string.IsNullOrEmpty(Session.Hydraulic.imgHydraulic1))
                        picConductor.picImg.Image = Image.FromFile(Session.Hydraulic.imgHydraulic1);
                    if (!string.IsNullOrEmpty(Session.Hydraulic.imgHydraulic2))
                        pic1st.picImg.Image = Image.FromFile(Session.Hydraulic.imgHydraulic2);
                    if (!string.IsNullOrEmpty(Session.Hydraulic.imgHydraulic3))
                        pic2nd.picImg.Image = Image.FromFile(Session.Hydraulic.imgHydraulic3);
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            SaveData();
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var FormationPressure = projects.Where(n => n.TableName == TableNames.Hydraulic.ToString()).FirstOrDefault();
            if (FormationPressure != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.Hydraulic.ToString(), JsonConvert.SerializeObject(Session.Hydraulic));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.Hydraulic.ToString(), JsonConvert.SerializeObject(Session.Hydraulic), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        private void StripMenuDel_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow item in dgv.SelectedRows)
                {
                    if (!item.IsNewRow)
                        dgv.Rows.Remove(item);
                }
                return;
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        #region ==重写键盘事件==
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            //在检测到按Ctrl+V键后，系统无法复制多单元格数据，重写ProcessCmdKey方法，屏蔽系统粘贴事件，使用自定义粘贴事件，在事件中对剪贴板的HTML格式进行处理，获取表数据，更新DataGrid控件内容

            if (keyData == (Keys.V | Keys.Control) && this.dgv.SelectedCells.Count > 0)
            {
                DGVHelper.CopyData(this.dgv);
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        #endregion

    }
}
